package com.yxhl.zoume.core.tripsmgmt.ui;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.support.design.widget.Snackbar;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.yxhl.zoume.R;
import com.yxhl.zoume.ZouMeApplication;
import com.yxhl.zoume.common.ui.custom.RevealLayout;
import com.yxhl.zoume.core.func.appraisal.model.CommentOnDriverEvent;
import com.yxhl.zoume.core.tripsmgmt.info.CommentEntrance;
import com.yxhl.zoume.core.tripsmgmt.presenter.ipresenter.ICommentPresenter;
import com.yxhl.zoume.data.rxbus.RxBus;

/**
 * Created by Sugarya.
 * 司机评价对话框
 */
public class CommentDriverPopupWindowHelper implements View.OnClickListener, PopupWindow.OnDismissListener {

    private static final String TAG = "CommentPopupHelper";
    private static final String COMMENT_MSG = "我是默认点评内容";
    private static CommentDriverPopupWindowHelper popupWindowHelper;
    private PopupWindow mPopupWindow;
    private ImageView mIvClose;
    private ImageView mCbComment1;
    private ImageView mCbComment2;
    private ImageView mCbComment3;
    private ImageView mCbComment4;
    private ImageView mCbComment5;
    private RevealLayout mTvComment;
    /**
     * 点评司机
     */
    private ICommentPresenter mICommentPresenter;
    /**
     * 评价星星选中的个数
     */
    private int checkState = 0;
    /**
     * 订单编号
     */
    private String mOrderId;
    /**
     * PopupWindow的根视图
     */
    private View mContentView;
    private Context mContext;

    private CommentDriverPopupWindowHelper(ICommentPresenter iCommentPresenter, Context context) {
        mPopupWindow = new PopupWindow(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        initializePopupWindow(iCommentPresenter, context);
    }

    public static CommentDriverPopupWindowHelper getInstance(ICommentPresenter iCommentPresenter, Context context) {
        if (popupWindowHelper == null) {
            synchronized (CommentDriverPopupWindowHelper.class) {
                if (popupWindowHelper == null) {
                    popupWindowHelper = new CommentDriverPopupWindowHelper(iCommentPresenter, context);
                }
            }
        }
        return popupWindowHelper;
    }

    private void initializePopupWindow(ICommentPresenter iCommentPresenter, Context context) {
        if (context != null) {
            mContentView = LayoutInflater.from(context).inflate(R.layout.trip_judge_popup, null);
            mPopupWindow.setFocusable(true);
            mPopupWindow.setOutsideTouchable(true);
            mPopupWindow.setContentView(mContentView);
            mPopupWindow.setBackgroundDrawable(new BitmapDrawable());
            mPopupWindow.setAnimationStyle(R.style.AnimationPopupWindow);
            mPopupWindow.setOnDismissListener(this);

            mIvClose = (ImageView) mContentView.findViewById(R.id.iv_trip_judge_cancel);
            mCbComment1 = (ImageView) mContentView.findViewById(R.id.iv_comment_1);
            mCbComment2 = (ImageView) mContentView.findViewById(R.id.iv_comment_2);
            mCbComment3 = (ImageView) mContentView.findViewById(R.id.iv_comment_3);
            mCbComment4 = (ImageView) mContentView.findViewById(R.id.iv_comment_4);
            mCbComment5 = (ImageView) mContentView.findViewById(R.id.iv_comment_5);
            mTvComment = (RevealLayout) mContentView.findViewById(R.id.reveal_trip_comment_complete);

            this.mContext = context;

            setOnClickListener();
        } else {
            throw new IllegalAccessError("Context cannot be null");
        }

        if (iCommentPresenter != null) {
            this.mICommentPresenter = iCommentPresenter;
        }
    }

    private void setOnClickListener() {
        mIvClose.setOnClickListener(this);
        mTvComment.setOnClickListener(this);
        mCbComment1.setOnClickListener(this);
        mCbComment2.setOnClickListener(this);
        mCbComment3.setOnClickListener(this);
        mCbComment4.setOnClickListener(this);
        mCbComment5.setOnClickListener(this);
    }

    public void showPopupWindow(View parent, String orderId) {
        mPopupWindow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
        this.mOrderId = orderId;
    }


    @Override
    public void onClick(View view) {
        if (view != null) {
            switch (view.getId()) {
                case R.id.iv_trip_judge_cancel:
                    closePopupWindow();
                    break;
                case R.id.reveal_trip_comment_complete:
                    if (checkState > 0) {
                        mICommentPresenter.commentOnDriver(mOrderId, checkState, COMMENT_MSG);
                    } else {
                        Snackbar.make(mContentView, ZouMeApplication.application.getString(R.string.trip_comment_hint), Snackbar.LENGTH_SHORT).show();
                    }
                    break;
                case R.id.iv_comment_1:
                    checkState = 1;
                    showCommentStateByCheckState(checkState);
                    break;
                case R.id.iv_comment_2:
                    checkState = 2;
                    showCommentStateByCheckState(checkState);
                    break;
                case R.id.iv_comment_3:
                    checkState = 3;
                    showCommentStateByCheckState(checkState);
                    break;
                case R.id.iv_comment_4:
                    checkState = 4;
                    showCommentStateByCheckState(checkState);
                    break;
                case R.id.iv_comment_5:
                    checkState = 5;
                    showCommentStateByCheckState(checkState);
                    break;
            }
        }
    }

    public void closePopupWindow() {
        if (mPopupWindow != null && mPopupWindow.isShowing()) {
            mPopupWindow.dismiss();
        }
    }

    /**
     * 网络请求,评价失败回调
     */
    public void onError() {
        if (mContext != null) {
            Toast.makeText(mContext, mContext.getString(R.string.trip_comment_on_error), Toast.LENGTH_SHORT).show();
        }
    }

    /**
     * 网络请求,评价成功回调
     */
    public void onCommentSuccess() {
        if (mContext != null) {
            Toast.makeText(mContext, mContext.getString(R.string.trip_comment_on_success), Toast.LENGTH_SHORT).show();
            RxBus.getInstance().send(new CommentOnDriverEvent(CommentEntrance.HISTORY_TRIP));
        }
    }

    /**
     * 网络请求,评价成功回调
     */
    public void onCommentSuccess(Activity activity) {
        if (mContext != null) {
            Toast.makeText(mContext, mContext.getString(R.string.trip_comment_on_success), Toast.LENGTH_SHORT).show();
        }
        if (activity != null) {
            activity.finish();
            activity.overridePendingTransition(R.anim.left_enter, R.anim.right_out);
        }
    }

    private void showCommentStateByCheckState(int checkState) {
        mCbComment1.setImageResource(checkState > 0 ? R.drawable.ic_comment_favorable : R.drawable.ic_comment_bad);
        mCbComment2.setImageResource(checkState > 1 ? R.drawable.ic_comment_favorable : R.drawable.ic_comment_bad);
        mCbComment3.setImageResource(checkState > 2 ? R.drawable.ic_comment_favorable : R.drawable.ic_comment_bad);
        mCbComment4.setImageResource(checkState > 3 ? R.drawable.ic_comment_favorable : R.drawable.ic_comment_bad);
        mCbComment5.setImageResource(checkState > 4 ? R.drawable.ic_comment_favorable : R.drawable.ic_comment_bad);
    }

    /**
     * 恢复到初始化状态
     */
    @Override
    public void onDismiss() {
        checkState = 0;
        showCommentStateByCheckState(0);
    }


}
